package com.tongji.housediy.view.titlewindows
{
	import com.tongji.housediy.model.GoodsModel;
	import com.tongji.housediy.model.SelectedObjectModel;
	import com.tongji.housediy.service.GoodsService;
	import com.tongji.housediy.view.PicSelectCompMediator;
	import com.tongji.housediy.view.components.Furniture;
	import com.tongji.housediy.view.components.RoomShape;
	import com.tongji.housediy.vo.Color;
	import com.tongji.housediy.vo.Model;
	import com.tongji.housediy.vo.Size;
	
	import flash.display.BlendMode;
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.filters.GlowFilter;
	
	public class GoodsInfoWindowMediator extends Mediator
	{
		[Inject]
		public var view:GoodsInfoTitleWindow;
		
		[Inject]
		public var model:GoodsModel;
		
		[Inject]
		public var service:GoodsService;
		
		[Inject]
		public var selectedModel:SelectedObjectModel;
		
		override public function onRegister():void
		{
			BindingUtils.bindProperty(view.goodsName, "text", model, "goodsName");
			BindingUtils.bindProperty(view.brandName, "text", model, "goodsBrand");
			BindingUtils.bindProperty(view.goodsSizeCmb, "dataProvider", model, "models");
			BindingUtils.bindProperty(view.goodsType, "text", model, "goodsType");
			BindingUtils.bindProperty(view.colorList, "dataProvider", model, "colorArray");
            
			for (var i:int = 0; i < model.models.length; i++)
			{
				if ((model.models.getItemAt(i) as Model).modelID == (selectedModel.selectedObj as RoomShape).selectedSizeID)
				{
					view.goodsSizeCmb.selectedIndex = i;
					break;
				}
			}
			
			for (i = 0; i < model.colorArray.length; i++)
			{
				if ((model.colorArray.getItemAt(i) as Color).colorID == (selectedModel.selectedObj as RoomShape).selectedColorID)
				{
					view.colorList.selectedIndex = i;
					break;
				}
			}
			
			mediatorMap.createMediator(view.picSelectedComp);
			
			view.closeBtn.addEventListener(MouseEvent.CLICK, closeWindowHandler);
			view.cancleBtn.addEventListener(MouseEvent.CLICK, closeWindowHandler);
			view.okBtn.addEventListener(MouseEvent.CLICK, okBtnClickHandler);
		}
		
		private function closeWindowHandler(event:MouseEvent):void
		{
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediatorByView(view.picSelectedComp);
			mediatorMap.removeMediatorByView(view);
		}
		
		private function okBtnClickHandler(event:MouseEvent):void
		{
			if ((view.goodsSizeCmb.selectedItem.modelID == (selectedModel.selectedObj as RoomShape).selectedSizeID) || (view.colorList.selectedItem.colorID == (selectedModel.selectedObj as RoomShape).selectedColorID))
			{}
			else
				service.getPicModel(view.goodsSizeCmb.selectedItem.modelID, view.colorList.selectedItem.colorID);			
			closeWindowHandler(event);
		}
	}
}